package com.jhx.mall.app.servlet;

import com.jhx.mall.app.service.DtsUserService;
import com.jhx.mall.app.service.impl.DtsUserServiceImpl;
import com.jhx.mall.entity.DtsCollect;
import com.jhx.mall.entity.DtsUser;
import com.jhx.mall.util.BaseServlet;
import com.jhx.mall.vo.DtsFootprintVo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "DtsUser", value = "/api/user/userInfo")
public class DtsUserServlet extends BaseServlet {
    private DtsUserService dtsUserService = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        dtsUserService = new DtsUserServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取opr请求参数
        String opr = request.getParameter("opr");

        if ("list".equals(opr)){
            List<DtsUser> list = dtsUserService.selectUserInfo(1);
            if (list!=null){
                ok(request,response,list);
            }else {
                error(request,response,list);
            }

        } else if ("myFootprint".equals(opr)){
            List<DtsFootprintVo> list = dtsUserService.selectMyFootprint(1);
            if (list!=null){
                ok(request,response,list);
            }else {
                error(request,response,list);
            }
        } else if ("addCollect".equals(opr)) {
            DtsCollect collect = new DtsCollect();
            int userId = Integer.parseInt(request.getParameter("userId"));
            int valueId = Integer.parseInt(request.getParameter("valueId"));
            int type = Integer.parseInt(request.getParameter("type"));
            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setType(type);
            if (dtsUserService.addCollect(collect)>0){
                ok(request,response,1);
            }else {
                error(request,response,0);
            }
        } else if ("delCollect".equals(opr)) {
            DtsCollect collect = new DtsCollect();
            int id = Integer.parseInt(request.getParameter("id"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            int valueId = Integer.parseInt(request.getParameter("valueId"));
            int type = Integer.parseInt(request.getParameter("type"));
            collect.setId(id);
            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setType(type);
            if (dtsUserService.delCollect(collect)>0){
                ok(request,response,1);
            }else {
                error(request,response,0);
            }
        }

    }
}
